package cn.zzm.core.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

import java.time.Duration;

/**
 * TODO(自定义连接池的mq连接工厂).
 *
 * <p>TODO(自定义连接池的mq连接工厂).
 *
 * <p>版权所有:Copyright (c) 2019<br/>
 *
 * @author 周昭名
 * @version V1.0
 * @since 2019/7/1 19:01
 */
@Configuration
public class ActiveMQRemoteConfig {
    @Value("${spring.activemq.remote.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.remote.user}")
    private String user;
    @Value("${spring.activemq.remote.password}")
    private String password;
    @Value("${spring.activemq.remote.poolSize}")
    private int poolSize;
    @Value("${spring.activemq.remote.maxSessionPerConnection}")
    private int maxSessionPerConnection;

    /**
      *
      *TODO(activeMQ连接工程).
      *
      *<p>TODO(activeMQ连接工程).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 17:10
    */
    @Bean(name="remoteActiveMQConnectionFactory")
    public ActiveMQConnectionFactory remoteActiveMQConnectionFactory(){
        ActiveMQConnectionFactory amf = new ActiveMQConnectionFactory(user,password,brokerUrl);
        return amf;
    }
    /**
      *
      *TODO(带连接池的连接工厂).
      *
      *<p>TODO(带连接池的连接工厂).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 17:11
    */
    @Bean(name="remotePoolConnectionFactory")
    public PooledConnectionFactory remotePoolConnectionFactory(){
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(remoteActiveMQConnectionFactory());
        pooledConnectionFactory.setMaxConnections(poolSize);//连接池大小
        pooledConnectionFactory.setMaximumActiveSessionPerConnection(maxSessionPerConnection);//每个连接池的最大session数
        return pooledConnectionFactory;
    }

    /**
      *
      *TODO(jms queue listener工厂).
      *
      *<p>TODO(jms queue listener工厂).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 17:13
    */
    @Bean(name="jmsRemoteQueueListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsRemoteQueueListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(remotePoolConnectionFactory());
        return factory;
    }

    /**
     *
     *TODO(jms topic listener工厂).
     *
     *<p>TODO(jms topic listener工厂).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/26 17:13
     */
    @Bean(name="jmsRemoteTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsRemoteTopicListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(remotePoolConnectionFactory());
        factory.setPubSubDomain(true);
        return factory;
    }

    /**
     *
     *TODO(带连接池的jmsTemplate).
     *
     *<p>TODO(带连接池的jmsTemplate).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/26 17:13
     */
    @Bean(name="jmsRemoteTemplate")
    public JmsTemplate jmsRemoteTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(remotePoolConnectionFactory());
        return jmsTemplate;
    }
}
